﻿/*
 *  Copyright (C) 2010 by Benjamin Nolmans <benjamin.nolmans@gmail.com>
 *  and Barbara Post <postb99@gmail.com>
 *  Copyright (C) 2010 by Barbara Post postb99@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System;
using System.Collections.Generic;
using OfficeWebUI.Ribbon;
using OsamesFrameworkCore;
using OsamesFrameworkCore.Logging;
using OsamesFrameworkCore.Plugins;
using OsamesFrameworkCore.Plugins.Management;
using OsamesFrameworkCore.Plugins.Management.Types;

namespace OsamesFrameworkUI.OfficeWebUiOfficeRibbon
{
    /// <summary>
    /// Rendering : a list of ribbon contexts (OfficeWebUi RibbonContext).
    /// </summary>
    public class RibbonContextManager : ILoggable
    {
        #region  properties
        /// <summary>
        /// List that business of this class will populate
        /// </summary>
        public List<RibbonContext> LstRibbonContext { get; private set; }

        #endregion

        #region contructor
        /// <summary>
        /// Constructor.
        /// </summary>
        public RibbonContextManager()
        {
            RefreshList();
        } 
        #endregion

        /// <summary>
        /// Asks plugins which should display in ribbon their ribbon context rendering.
        /// This method should be called when a plugin is loaded/unloaded to refresh list.
        /// </summary>
        internal void RefreshList()
        {
            Log(LogLevel.Trace, "RibbonContextsManager : RefreshList");

            LstRibbonContext = new List<RibbonContext>();

            Dictionary<string,AvailablePlugin> dicPlugins = PluginsManager.Instance.DicAvailablePluginSet;
            
            foreach (KeyValuePair<string, AvailablePlugin> kvpApPlugin in dicPlugins)
            {
                Log(LogLevel.Trace, "AvailablePlugin : {0}", new object[]{kvpApPlugin.ToString()});

                IWebPlugin webPlugin = kvpApPlugin.Value.WebPlugin; //aPlugin.WebPlugin;
                if (webPlugin == null) continue;

                RibbonContext rtTab = webPlugin.GetRibbonContextRendering();

                Log(LogLevel.Trace, "Plugin renders a tab ? : {0}", new object[]{rtTab != null});

                if (rtTab == null) continue;

                Log(LogLevel.Trace, "Ribbon tab text : '{0}'", new object[]{rtTab.Text});

                LstRibbonContext.Add(rtTab);
            }
        }

        /// <summary>
        /// Logging facility.
        /// </summary>
        /// <param name="strMessage_">Message</param>
        /// <param name="strMessageDetails_">Optional array of message details. In this case, strMessage_ parameter contains placeholders, each matching a value in strMessageDetails_</param>
        /// <param name="eLogLevel_">Log level</param>
        public void Log(LogLevel eLogLevel_, string strMessage_, object[] strMessageDetails_ = null)
        {
            LoggerService.Log(this, eLogLevel_, strMessage_, strMessageDetails_);
        }

        /// <summary>
        /// Logging facility.
        /// </summary>
        /// <param name="eLogLevel_">Osames log level</param>
        /// <param name="strMessage_">Message</param>
        /// <param name="ex_">Exception we will log message and stack trace of</param>
        public void Log(LogLevel eLogLevel_, string strMessage_, Exception ex_)
        {
            LoggerService.Log(this, eLogLevel_, strMessage_, ex_);
        }
    }
}
